/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.UserDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.model.User;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class UserDaoImpl extends BaseDaoImpl implements UserDao {
	public List getList(boolean justEnabled, boolean includeAdmin,
			String searchKey, String orderBy) {
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		String sql = "select u.id as u_id,u.user_name,s.staffer_name,u.use_validity,u.validity,u.enabled,\'\' as overdue,d.department_name,s.possie,s.id as s_id from user u left join staffer s on s.id=u.staffer_id left join department d on d.id=s.department_id where 1=1";
		if (!includeAdmin) {
			sql = sql + " and u.user_type!=" + 1;
		}

		if (justEnabled) {
			sql = sql
					+ " and u.enabled=\'Y\' and (u.use_validity=\'N\' or u.use_validity=\'Y\' and (u.validity is null or u.validity>:validity))";
		}

		if (searchKey != null && !searchKey.equals("")) {
			sql = sql
					+ " and ((u.user_name like :userName) or (s.staffer_name like :stafferName) or (s.name_spell like :nameSpell))";
		}

		if (orderBy.equals("USER_NAME")) {
			sql = sql + " order by u.user_name";
		} else {
			sql = sql + " order by d.department_name,s.name_spell";
		}

		Session sess = null;

		List arg8;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			if (justEnabled) {
				q.setDate("validity", new Date());
			}

			if (searchKey != null && !searchKey.equals("")) {
				q.setString("userName", "%" + searchKey + "%");
				q.setString("stafferName", "%" + searchKey + "%");
				q.setString("nameSpell", "%" + searchKey + "%");
			}

			arg8 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg8;
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.User where id = :id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int deleteStafferUser(final long stafferId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.User where stafferId = :stafferId";
						return new Integer(session.createQuery(hql)
								.setLong("stafferId", stafferId)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int unenabledStafferUser(final long stafferId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.User set enabled=\'N\' where stafferId = :stafferId";
						return new Integer(session.createQuery(hql)
								.setLong("stafferId", stafferId)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public User get(String userName) {
		String[] paramNames = new String[]{"userName"};
		Object[] values = new Object[]{userName};
		List list = this.getHibernateTemplate().findByNamedParam(
				"from com.minierp.model.User u where u.userName=:userName",
				paramNames, values);
		return !list.isEmpty() ? (User) list.get(0) : null;
	}

	public User getAdmin() {
		String sql = "from com.minierp.model.User u where u.userType=1";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			List list = q.list();
			if (!list.isEmpty()) {
				User arg5 = (User) list.get(0);
				return arg5;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return null;
	}
}